// [Reference] http://www.jb51.net/article/50766.htm

function imgReader(imgData) {
    
    let file = imgData.getAsFile()
    let reader = new FileReader()

    reader.onload = function (ev) {
        let img = new Image()
        img.src = ev.target.result
        console.log(img.src)
        $("#send-message pre").append(img)
    }

    reader.readAsDataURL(file)
}

function pasteEventHandler(ev) {
    
    console.log("an paste event occur")
    console.log(ev.clipboardData)

    if (!ev.clipboardData) return

    let items = ev.clipboardData.items
    if (!items) return

    let item = items[0]
    let dataTypes = ev.clipboardData.dataTypes || []
    for (let i = 0, len = dataTypes.length; i < len; i++) {
        if (dataTypes[i] === 'Files') {
            item = items[i]
            break
        }
    }

    if (item && item.kind === 'file' && item.type.match(/^image\//i)) {
        console.log("go here")
        imgReader(item)
    }
}
